var Component;
var Model;
var Browser;
var Fn;
var openId, timeStamp, channel,tempCSTNO, DYRECNO, type,action,checkFlag,MyParam,MOBILE,JUrl;
var isGetCheck;
var changDefNum;
var disTime = 60;// 定义获得一次短信验证码后再次获取需要等待的时间
var clickFlag = 0;// 未点击获取验证码
//查询签约状态
var url_PEMIQYSN = "../rest/loginNew/gst";
//预留手机登录前获取动态验证码
var url_HQSJYZMB = "../rest/mbck/verifyLogin";
//预留手机登录
var url_PEMIOBLG = "../rest/loginNew/d";
//***************************初始化页面**************************
function initPage(urlParam, param) {
	if ($("[data-debug]").attr("data-debug") == "@simdata") {
		url_PEMIQYSN = "sign.do?act=PEMIQYSN";
		url_HQSJYZMB = "sign.do?act=HQSJYZMB";
		url_PEMIOBLG = "sign.do?act=PEMIOBLG";
	}
	Component = CBUI.Component;
	Model = CBUI.Model;
	Browser = CBUI.Browser;
	Fn = CBUI.Fn;
	Fn.setModels({

	});
	
	var nick = ""; // 微信昵称
	isGetCheck = false;
	openId = urlParam.FU; // 微信号
	channel = urlParam.channel; // 渠道ID
	timeStamp = urlParam.TS; // 时间戳
	action = urlParam.action; // 操作类型
	type = urlParam.type;
	JUrl = urlParam.JUrl;
	checkFlag="1";//是否需要输入验证码 0：需要 1：不需要
	changDefNum = true;
	isGetCheck = false;
	MyParam=param;
	if ((channel == config.channl.weixin)||(channel == config.channl.ylj)) {
		deleteMenu();
	}

	// 定制组件预留手机登录方式
	Component.DefaultBank = Component.Page.extend({
				
			});

	MSG.loadMsg();
	$.ajax({
		type : 'post',
		url : '../rest/gnick/gn',
		data : {
			'openId' : escape(openId),
			'channelId' : escape(channel)
		},
		dataType : "json",
		success : function(response) {
			
			nick = response.data.nick;
			var model = new Model.CBJSON({

			}		, {
						url : url_PEMIQYSN,
						param : {
							'type':type,
							'channelId' : escape(channel),
							'openId' : escape(openId)
						},
						onComplete : function(s, data) {
							MSG.closeAlert();						
							var mobile = data.mobile;
							var mobileHid = data.mobileHid;
							var sign= data.SIGNCLASS;
							loadDBlogin(nick,mobile,mobileHid,sign);
														
						}
			});
			model.fetch();		
		}
	});

	
}
//*****************************end**********************************

function loadDBlogin(nick,mobile,mobileHid,sign)
{
	var option;
	var sendData = {
		TYPE : ""
	};
	var initVal = {
		TEXT : "Loading...",
		Nick : nick,
		MobileHid:mobileHid,
		Mobile:mobile,
		Sign : sign
	};
	option = {
		refresh : {
			initVal : initVal,
			param : sendData
		}
	};
	Browser.history_goto("defaultBank", option);
}
/***********************************手机号码变更处理******************************************/
/**
 * li点击后修改手机号码
 */
function changeNum()
{
	if(changDefNum)
	{
	   if(oc != 0)
	   {
		   MSG.confirmMsg("修改手机号后原验证码将失效，是否确认修改？", config.alertType.warn);
			MSG.success = function() {
				// 将手机号码输入框设置成可编辑
				oc = 0;
				$("#dbphonenum").removeAttr("readOnly");
				$("#phonereturn").css("display","none");
				$("#editephonep").css("display","none");
				$("#dbphonenum").css("display","");	
				$("#dbphonenum").val("");
				$("#dbcheckno").val("");
				$("#alertmsg").css("display", "none");
				$("#alertmsg").html("");
				$("#dbgetcheck").html('获取验证码');
				$("#dbgetcheck").removeClass("regetCheck");
				$("#dbgetcheck").addClass("getCheck");
				$("#dbgetcheck").removeAttr('disabled');
				
				changDefNum = false;
				tempCSTNO = "";
				DYRECNO = "";
				MSG.closeAlert();
				clickFlag = 0;			
			};
	   }else{
		   $("#phonereturn").css("display","none");
		   $("#editephonep").css("display","none");
		   $("#dbphonenum").css("display","");	
		   $("#dbphonenum").val("");
		   $("#dbcheckno").val("");
		   changDefNum = false;
	   }
	}
}
var oc = 0;
/**
 * input点击更改手机输入框
 */
function changePhone() {

	if (oc != 0) {
		MSG.confirmMsg("修改手机号后原验证码将失效，是否确认修改？", config.alertType.warn);
		MSG.success = function() {
			// 将手机号码输入框设置成可编辑
			oc = 0;
			$("#dbgetcheck").html('获取验证码');
			$("#dbgetcheck").removeClass("regetCheck");
			$("#dbgetcheck").addClass("getCheck");
			$("#dbgetcheck").removeAttr('disabled');
			$("#alertmsg").css("display", "none");
			$("#alertmsg").html("");
			$("#dbphonenum").removeAttr("readOnly");
			$("#dbphonenum").val("");
			$("#dbcheckno").val("");
			tempCSTNO = "";
			DYRECNO = "";
			MSG.closeAlert();
			clickFlag = 0;			
		};
	}

}

//******************************跳转主菜单*****************************
/*
 * 主菜单
 */
function mainMenu(param) {

	param = encodeURIComponent(param);
	if(action == undefined || action == "")
	{
		if(JUrl == undefined || JUrl == "")
		{
			location.href = "../cardSignY/mainMenu.html?param=" + param;
		}else{
			location.href = ".." +JUrl + "?param="+param;
		}
	}else if(action == '0')
	{
		location.href = "../cardSignY/addCard.html?param=" + param;
	}
	else if(action == '1')
	{
		location.href = "../cardSignY/setDefCard.html?param=" + param;
	}
	else if(action == '2')
	{
		location.href = "../cardSignY/delCard.html?param=" + param;
	}else if(action == '3')
	{
		location.href = "../cardSignY/changePush.html?param=" + param;
	}else{
//		location.href = "../cardSign/mainMenu.html?param=" + param;
		
	}
	
}
//*****************************end**********************************




//*************************获取手机动态验证码*****************************

function f_timeout() {
	var mobile = $("#dbphonenum").val();
	if (mobile == undefined || mobile == "") {
		toastMessage("手机号码不能为空");
		return;
	}
	else if (mobile.length != 11 || !dataUtils.isDigit(mobile)) {
		toastMessage("手机号码有误");
		return;
	} 
	if(isGetCheck == true)
	{
		return;
	}
	isGetCheck = true;
	oc++;
	$("#dbphonenum").attr("readOnly", true);
	$("#dbgetcheck").attr('disabled', true);
	$("#dbgetcheck").removeClass("getCheck");
	$("#dbgetcheck").addClass("regetCheck");	
	$("#alertmsg").css("display", "none");
	$("#dbcheckno").val("");
	var model = new Model.CBJSON({

	}		, {
				url : url_HQSJYZMB,
				param : {
					'type':type,
					'mobile':mobile,
					'channelId' : escape(channel),
					'openId' : escape(openId)
				},
				onComplete : function(s, data) {
					MSG.closeAlert();
					isGetCheck = false;
					if (data.result == "success") {
						if (data.TMPCSTNO == undefined || data.TMPCSTNO == "") {
							$("#dbgetcheck").removeClass("regetCheck");
        					$("#dbgetcheck").addClass("getCheck");
        					$("#dbgetcheck").removeAttr('disabled');
							tempCSTNO = "";
							DYRECNO = "";
							MOBILE="";
							clickFlag = 0;
							toastMessage("验证码发送失败");

						} else {
							clickFlag = 1;
							toastMessage("验证码已发送");
							var timer = setInterval(function() {
								$("#dbgetcheck").attr('disabled', true);
								$("#dbgetcheck").removeClass("getCheck");
								$("#dbgetcheck").addClass("regetCheck");
								$("#dbgetcheck").html(disTime);
								if(clickFlag==0){
									clearInterval(timer);									
									$("#dbgetcheck").html('获取验证码');
									$("#dbgetcheck").removeClass("regetCheck");
									$("#dbgetcheck").addClass("getCheck");
									$("#dbgetcheck").removeAttr('disabled');
									$("#alertmsg").css("display", "none");
									$("#alertmsg").html("");
									disTime = 60;
									isGetCheck = false;
								}
								if (disTime == 0 ) {
									clearInterval(timer);
									$("#dbgetcheck").html('重新获取');
									$("#dbgetcheck").removeClass("regetCheck");
									$("#dbgetcheck").addClass("getCheck");
									$("#dbgetcheck").removeAttr('disabled');
									disTime = 60;
									isGetCheck = false;
								}
								
								disTime--;

							}, 1000);
							if(oc == 0)
							{
							    tempCSTNO = "";
							    DYRECNO = "";
							    clickFlag = 0;
							    
							}else{
								 tempCSTNO = data.TMPCSTNO;
								 DYRECNO = data.DYRECNO;
							}
							MOBILE=data.MOBILE;
							var msg="<div class = 'checkNoMsg'>动态密码已发送至手机"+MOBILE+",请在<span style='color:#009dd9;'>180秒</span>内输入。</div>";
							$("#alertmsg").css("display", "");
							$("#alertmsg").html(msg);
						}

						var sendData = {
							TYPE : "A"
						};
						var initVal = {
							TEXT : "Loading..."

						};

						option = {
							refresh : {
								initVal : initVal,
								param : sendData
							}
						};
					} else {
						$("#dbgetcheck").removeClass("regetCheck");
    					$("#dbgetcheck").addClass("getCheck");
    					$("#dbgetcheck").removeAttr('disabled');
    					oldShowError(s,data);
					}
				}
			});
	model.fetch();

}
//****************************动态验证码end***************************


//****************************预留电话号登录方法*****************************
function HomeSub(){

	var checkNo=$("#dbcheckno").val().trim();
	var mobile = $("#dbphonenum").val().trim();
	if (mobile == undefined || mobile == "") {
		toastMessage("手机号码不能为空");
		return;
	}
	else if (mobile.length != 11 || !dataUtils.isDigit(mobile)) {
		toastMessage("手机号码有误");
		return;
	} 
	else if ((tempCSTNO == undefined || tempCSTNO == "") && clickFlag == 0) {
		toastMessage("请先获取动态验证码");
		return;
	} 
	else if ((tempCSTNO == undefined || tempCSTNO == "") && clickFlag == 1) {
		toastMessage("动态验证码发送中，请稍候");
		return;
	} 
	else if (checkNo == undefined || checkNo == "") {
		toastMessage("动态验证码不能为空");
		return;
	}
	
	else if(!dataUtils.isDigit(checkNo) || checkNo.length != 6){
		toastMessage("动态验证码输入有误");
		return;
	}else{
		MSG.loadMsg();
		var model = new Model.CBJSON({}, {
					url : url_PEMIOBLG,
					param : {
						
						'checkNo' : escape(checkNo),
						'openId' : escape(openId),
						'channel' : escape(channel),
						'tempCSTNO' : escape(tempCSTNO),
						'DYRECNO' : escape(DYRECNO),
						'mobile' : mobile,
						'type' : type,					
						'param' : MyParam
					},
					onComplete : function(s, data) {
						MSG.closeAlert();
						if (data.result == "success") {
							var params = data.params.param;
							mainMenu(params);
						} else {
							oldShowError(s,data);
						};

					}
				});
		model.fetch();
	}

}


//*******************************end****************************


function initMyPage(IntA){	// 登录页面跳转
	 
	var p = encodeURIComponent(MyParam);
	if(IntA==1){
		location.href = "../cardSign/DBLogin.html?param=" + p;
	}else if(IntA==2){
		location.href = "../cardSign/PBLogin.html?param=" + p;
	}else if(IntA==3){
		location.href = "../cardSign/MBLogin.html?param=" + p;
	}else if(IntA==4){
		location.href = "../cardSign/ELogin.html?param=" + p;
	}else{
		location.href = "../cardSign/appDnoFrame.html?param=" + p;
	}
}
